/**
 * Http class
 */

import config from "../config/config";
import Crypt from "./Crypt";
import key from '../config/constant'

// storage key
const tokenKey = key.tokenKey
const sessionKey = key.sessionKey
const referrerKey = key.referrerKey

export default class Http {

  request({url, data = {}, method = 'POST'}) {
    return new Promise((resolve, reject) => {

      // flag
      let loadingFlag = true
      setTimeout(() => {
        if (loadingFlag) {
          wx.showLoading({
            title: '加载中...'
          })
        }
      }, 2000)

      // wx.showLoading({
      //   title: '加载中...'
      // })
      wx.request({
        url: `${config.app_base_url}${url}`,
        method,
        data: {
          ...data,
          driver: wx.getStorageSync('driver')
        },
        header: {
          // 'content-type': 'application/json',
          'X-Token-bfc': this.getToken() || '',
          // 解决表情字符
          'content-type': 'application/json;charset=utf8mb4'
        },
        success: response => {
          loadingFlag = false
          wx.hideLoading()
          const { data } = response
          if (data.code === 0) {
            resolve(data)
          } else if (data.code === -2) {
            // 未登录
            wx.redirectTo({
              url: '/sub_main/pages/user/login/index'
            })
          } else if (data.code === -300) {
            // avatar && nickname
            wx.redirectTo({
              url: '/sub_main/pages/user/avatar/index'
            })
          } else if (data.code === -100) {
            // 重新获取电话
            // 不能这么操作, 审核通不过的
            // wx.redirectTo({
            //   url: '/sub_main/pages/user/mobile/index'
            // })

            // 数据库删除用户了
            // 推荐人不能忘了
            const ref = this.getReferrer()
            wx.clearStorageSync()
            this.setReferrer(ref)
            const time = 1000
            wx.showToast({
              title: '获取手机号码失败',
              icon: 'none',
              duration: time,
              mask: true
            })

            wx.switchTab({
              url: '/pages/home/index'
              // url: '/pages/index/index'
            })
          } else if (data.code === -1 || data.code === -7) {
            wx.showToast({
              title: data.message,
              icon: 'none',
              duration: 1000
            })
          } else {
            // console.log(data.message)
            reject(data.message)
          }
        },
        fail: err => {
          loadingFlag = false
          wx.hideLoading()
          reject(err)
        },
        complete: () => {
        }
      })
    })
  }

  setToken(token) {
    const data = Crypt.encrypt(token)
    wx.setStorageSync(tokenKey, data)
  }

  getToken() {
    const data = wx.getStorageSync(tokenKey)
    return Crypt.decrypt(data)
  }

  setSessionKey(data) {
    wx.setStorageSync(sessionKey, data)
  }

  getSessionKey() {
    return wx.getStorageSync(sessionKey)
  }

  setReferrer(id) {
    wx.setStorageSync(referrerKey, id)
  }

  getReferrer() {
    return wx.getStorageSync(referrerKey)
  }

  test() {
    return this.request({
      url: '/site/index',
      data: { title: '123' },
      method: 'POST'
    })
  }

}
